package org.vast.ows.sps;

import org.vast.cdm.common.CDMException;
import org.vast.ogc.OGCRegistry;
import org.vast.ows.AbstractResponseWriter;
import org.vast.ows.OWSException;
import org.w3c.dom.*;
import org.vast.util.DateTime;
import org.vast.util.DateTimeFormat;
import org.vast.xml.DOMHelper;

public class GetStatusResponseWriterV20 extends AbstractResponseWriter<GetStatusResponse>
{

	protected SPSCommonWriterV20 commonWriter = new SPSCommonWriterV20();
	
	
	public Element buildXMLResponse(DOMHelper dom, GetStatusResponse response, String version) throws OWSException
	{
		try
		{
			dom.addUserPrefix("sps", OGCRegistry.getNamespaceURI(SPSUtils.SPS, version));
			
			// root element
			Element rootElt = dom.createElement("sps:" + response.getMessageType());
			
			// latest response time
			DateTime latestResp = response.getLatestResponseTime();
			if (latestResp != null)
				dom.setElementValue(rootElt, "sps:latestResponseTime",
						DateTimeFormat.formatIso(latestResp.getJulianTime(), 0));
			
			// status report
			for (StatusReport report : response.getReports()){
			    Element reportElt = commonWriter.writeReport(dom, report);
			    Element resElt = dom.addElement(rootElt, "sps:result");
			    resElt.appendChild(reportElt);
			}
			
			return rootElt;
		}
		catch (CDMException e)
		{
			throw new SPSException(e);
		}
	}
}
